﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SocialPlatformSDK.Deserializer;
using SocialPlatformSDK.Hammock;
using SocialPlatformSDK.Model;
using SocialPlatformSDK.Services;
using SocialPlatformSDK.Services.Util;

namespace SocialPlatformSDK
{
    /// <summary>
    /// TencentAPIService 包含了 微博 相关 API.
    /// T 是 ServiceArgument
    /// </summary>
    public class TencentAPIService<T> : BaseService<T>
        where T : ServiceArgument, new()
    {

        public TencentAPIService(AccessToken accessToken, IOAuthConfigruation oAuthConfigruationBase)
            : base(accessToken, oAuthConfigruationBase)
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public TencentAPIService()
            : base(null, null)
        { }

        private bool ConverArgument(ref ServiceArgument argment)
        {
            bool res = false;
            TencentServiceArgument arg = argment as TencentServiceArgument;
            if (arg != null)
            {
                res = true;
                arg.Oauth_Consumer_Key = this.oAuthConfigruationBase.APP_KEY;
                arg.AccessToken = this.oAuthConfigruationBase.AccessToken;
                TencentCodeToken codetoken = this.oAuthConfigruationBase.CodeToken as TencentCodeToken;
                if(null!=codetoken)
                    arg.OpenId = codetoken.Openid;
               // arg.ClientIp = "117.135.159.4";
            }
            return res;
        }
        public void Add(ServiceArgument argment, Action<Callback<object>> callback)
        {
            if (!ConverArgument(ref argment))
                return;
            this.Post("t/add", argment, (request, response, userState) =>
            {
                if (null != callback)
                {
                    callback(new Callback<object>(null));
                }
            });

        }

        /// <summary>
        /// 发表一条带图片的微博.
        /// </summary>
        /// <param name="argment">参数列表，Content和UploadPic为必填参数.</param>
        /// <param name="callback">无返回数据.</param>
        public void AddPic(ServiceArgument argment, Action<Callback<object>> callback)
        {
            if (!ConverArgument(ref argment))
                return;
            this.Post("t/add_pic", argment, (request, response, userState) =>
            {
                if (null != callback)
                {
                    callback(new Callback<object>(null));
                }
            });
        }


        public void UserInfo(Action<Callback<User>> callback)
        {
            ServiceArgument argment = new T();
            if (!ConverArgument(ref argment))
                return;
            this.Get("user/info", argment, (request, response, userState) =>
            {
                if (null != callback &&0!= response.Content.Length)
                {
                    User user = null;
                    BaseDeserializer serializer = DeserializerManager.Instance.BuildUserDeserializer();
                    user = serializer.Read(response.Content) as User;

                    if (null != callback)
                    {
                        callback(new Callback<User>(user));
                    }
                }
            });
        }


        /// <summary>
        /// 我发表时间线.
        /// </summary>
        /// <param name="argment">参数列表, 可以为 null.</param>
        /// <param name="callback">回调返回我发表的时间线数据</param>
        public void BroadcastTimeline(ServiceArgument argment, Action<Callback<StatusCollection>> callback)
        {
            if (!ConverArgument(ref argment))
                return;
            this.Get("statuses/broadcast_timeline", argment, (request, response, userState) =>
            {
                InternalCallback(response, callback);
            });
        }

        /// <summary>
        /// 主页时间线.
        /// </summary>
        /// <param name="argment">参数列表, 可以为 null.</param>
        /// <param name="callback">回调返回主页时间线数据.</param>
        public void HomeTimeline(ServiceArgument argment, Action<Callback<StatusCollection>> callback)
        {
            if (!ConverArgument(ref argment))
                return;
            this.Get("statuses/home_timeline", argment, (request, response, userState) =>
            {
                InternalCallback(response, callback);
            });
        }

        /// <summary>
        /// 我发表时间线索引.
        /// </summary>
        /// <param name="argment">参数列表, 可以为 null.</param>
        /// <param name="callback">回调返回我发表时间线索引数据.</param>
        public void BroadcastTimelineIds(ServiceArgument argment, Action<Callback<StatusCollection>> callback)
        {
            if (!ConverArgument(ref argment))
                return;
            this.Get("statuses/broadcast_timeline_ids", argment, (request, response, userState) =>
            {
                InternalCallback(response, callback);
            });
        }

        /// <summary>
        /// 主页时间线索引.
        /// </summary>
        /// <param name="argment">参数列表, 可以为 null.</param>
        /// <param name="callback">回调返回主页时间线索引数据.</param>
        public void HomeTimelineIds(ServiceArgument argment, Action<Callback<StatusCollection>> callback)
        {
            if (!ConverArgument(ref argment))
                return;
            this.Get("statuses/home_timeline_ids", argment, (request, response, userState) =>
            {
                InternalCallback(response, callback);
            });
        }

        /// <summary>
        /// 用户提及时间线.
        /// </summary>
        /// <param name="argment">参数列表, 可以为 null.</param>
        /// <param name="callback">回调返回用户提及时间线数据.</param>
        public void MentionsTimeline(ServiceArgument argment, Action<Callback<StatusCollection>> callback)
        {
            if (!ConverArgument(ref argment))
                return;
            this.Get("statuses/mentions_timeline", argment, (request, response, userState) =>
            {
                InternalCallback(response, callback);
            });
        }

        /// <summary>
        /// 用户提及时间线索引.
        /// </summary>
        /// <param name="argment">参数列表, 可以为 null.</param>
        /// <param name="callback">返回用户提及时间线索引数据.</param>
        public void MentionsTimelineIds(ServiceArgument argment, Action<Callback<StatusCollection>> callback)
        {
            if (!ConverArgument(ref argment))
                return;
            this.Get("statuses/mentions_timeline_ids", argment, (request, response, userState) =>
            {
                InternalCallback(response, callback);
            });
        }

        private void InternalCallback(RestResponse response, Action<Callback<StatusCollection>> callback)
        {
            lock (this)
            {
                StatusCollection list = null;

                BaseDeserializer serializer = DeserializerManager.Instance.BuildStatusDeserializer();
                list = serializer.ReadList(response.Content) as StatusCollection;

                if (null != callback)
                {
                    callback(new Callback<StatusCollection>(list));
                }
            }
        }
    }
}
